package com.jifeng.sterilizer.service.impl;

import com.jifeng.sterilizer.entity.ControllerEntity;
import com.jifeng.sterilizer.utils.HttpUtils;
import com.jifeng.sterilizer.utils.PageUtils;
import com.jifeng.sterilizer.utils.Query;
import com.jifeng.sterilizer.utils.RegexUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.jifeng.sterilizer.dao.VwControllerDao;
import com.jifeng.sterilizer.entity.VwControllerEntity;
import com.jifeng.sterilizer.service.VwControllerService;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


@Service("vwControllerService")
@Slf4j
public class VwControllerServiceImpl extends ServiceImpl<VwControllerDao, VwControllerEntity> implements VwControllerService {

    /**
     * @param params {farm: '', type: '', number: '', name: '', status: '', produce_time: '', sale_time: '',cur_page,limit}
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        try {
            QueryWrapper<VwControllerEntity> queryWrapper = new QueryWrapper<>();
            Object farm = params.get("farm");
            Object type = params.get("type");
            Object number = params.get("number");
            Object name = params.get("name");
            Object status = params.get("status");
            Object produce_time = params.get("produce_time");
            Object sale_time = params.get("sale_time");
            if (!farm.equals("")) {
                queryWrapper.like("farm", farm);
            }
            if (!type.equals("")) {
                queryWrapper.like("type", type);
            }
            if (!name.equals("")) {
                queryWrapper.like("name", name);
            }
            if (!status.equals("")) {
                queryWrapper.eq("status", status);
            }
            if (!produce_time.equals("")) {
                queryWrapper.like("produce_time", produce_time);
            }
            if (!sale_time.equals("")) {
                queryWrapper.like("sale_time", sale_time);
            }
            if (!number.equals("")) {
                if (!RegexUtils.isNumber(number.toString(), 2)) {
                    log.error("方法：{}，错误信息：{}", "搜索中控器列表", "中控器编号格式错误");
                    return null;
                }
                queryWrapper.eq("number", number);
            }
            IPage<VwControllerEntity> page = this.page(
                    new Query<VwControllerEntity>().getPage(params),
                    queryWrapper
            );
            HttpServletRequest request = getRequest();
            // 获得token中的用户信息
            Map tokenInfo = HttpUtils.getTokenInfo(request);
            Object farmId = tokenInfo.get("farmId");
            List<VwControllerEntity> records = page.getRecords();
            List<VwControllerEntity> removeList = new ArrayList<>();
            records.forEach(vwControllerEntity -> {
                //  如果农场id！=用户id则去除数据
                if (!vwControllerEntity.getFarmId().toString().equals(farmId.toString())) {
                    removeList.add(vwControllerEntity);
                }
            });
            records.removeAll(removeList);
            page.setRecords(records);
            page.setTotal(records.size());
            return new PageUtils(page);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "搜索中控器列表", e.getMessage());
            return null;
        }
    }

    private HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    }
}